package com.itheima.servlet;

import com.itheima.bean.Student;
import jdk.nashorn.api.tree.WhileLoopTree;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/listStudentServlet")
public class ListStudentServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        字符输入流读取
        BufferedReader br = new BufferedReader(new FileReader("D:\\add-stu.txt"));
        //创建集合对象
        ArrayList<Student> list = new ArrayList<>();

        String line;
        while((line=br.readLine())!=null){
            Student stu = new Student();
            String[] arr = line.split(",");
            stu.setName(arr[0]);
            stu.setAge(Integer.parseInt(arr[1]));
            stu.setScore(Integer.parseInt(arr[2]));
            list.add(stu);
            //遍历集合
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            for (Student s : list) {
                pw.write(s.getName()+","+s.getAge()+","+s.getScore());
                pw.write("<br>");
            }

        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
